package vn.com.demand.util;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.eclipse.jface.dialogs.MessageDialog;

public class Validator {
	
	public static boolean isValidContractNumber(String contractNumber) {
		String sql = "select contractnumber from Demand_ContractInformation where ltrim(rtrim(contractnumber)) = ?";
		QueryRunner runner = new QueryRunner(DpUtils.getDataSource());
		try {
		String contractnumber = (String) runner.query(sql, contractNumber, new ResultSetHandler() {

				public Object handle(ResultSet rs) throws SQLException {
					String code = null;
					while(rs.next()) {
						code = rs.getString("contractnumber");
					}
					return code;
				}

			});
		return contractnumber != null;
		} catch (Exception e) {
			MessageDialog.openError(null, "Error", e.getMessage());
		}
		return false;
	}

	public static boolean isValidJournalCode(String code) {
		String sql = "select journalcode from Demand_JournalList where ltrim(rtrim(journalcode)) = ?";
		QueryRunner runner = new QueryRunner(DpUtils.getDataSource());
		try {
		String journalcode = (String) runner.query(sql, code, new ResultSetHandler() {

				public Object handle(ResultSet rs) throws SQLException {
					String code = null;
					while(rs.next()) {
						code = rs.getString("journalcode");
					}
					return code;
				}

			});
		return journalcode != null;
		} catch (Exception e) {
			MessageDialog.openError(null, "Error", e.getMessage());
		}
		return false;
	}
	
	public static boolean isValidAccountNumber(String accountNumber) {
		String sql = "select reknr from Demand_LedgerList where ltrim(rtrim(reknr)) = ?";
		QueryRunner runner = new QueryRunner(DpUtils.getDataSource());
		try {
		String reknr = (String) runner.query(sql, accountNumber, new ResultSetHandler() {

				public Object handle(ResultSet rs) throws SQLException {
					String code = null;
					while(rs.next()) {
						code = rs.getString("reknr");
					}
					return code;
				}

			});
		return reknr != null;
		} catch (Exception e) {
			MessageDialog.openError(null, "Error", e.getMessage());
		}
		return false;
	}

	public static boolean isValidIssuingPurpose(String accountNumber) {
		String sql = "select selcode from Demand_Issuingpurpose where ltrim(rtrim(selcode)) = ?";
		QueryRunner runner = new QueryRunner(DpUtils.getDataSource());
		try {
		String selcode = (String) runner.query(sql, accountNumber, new ResultSetHandler() {

				public Object handle(ResultSet rs) throws SQLException {
					String code = null;
					while(rs.next()) {
						code = rs.getString("selcode");
					}
					return code;
				}

			});
		return selcode != null;
		} catch (Exception e) {
			MessageDialog.openError(null, "Error", e.getMessage());
		}
		return false;
	}
	
	public static boolean isMoreThanOneSalesOrder(String contractCode) {
		String sql = "select distinct(SalesOrderNumnber) as salesorder from Demand_SalesOrder where ltrim(rtrim(contractcode)) = ?";
		QueryRunner runner = new QueryRunner(DpUtils.getDataSource());
		try {
			return (Boolean) runner.query(sql, contractCode, new ResultSetHandler() {
				int i = 0;
				public Object handle(ResultSet rs) throws SQLException {
					while (rs.next()) {
						i++;
					}
					return i > 1;
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
			MessageDialog.openError(null, "Error", e.getMessage());
		}
		return false;
	}
	
}
